import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BusiProvider } from '../../providers/busi/busi';
import { NativeProvider } from '../../providers/native/native';
import { FileChooser } from '@ionic-native/file-chooser';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { FilePath } from '@ionic-native/file-path';
import { a } from '@angular/core/src/render3';
import { Utils } from '../../common/util';
import { utc } from 'moment';


/**
 * Generated class for the AddmettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-addmetting',
  templateUrl: 'addmetting.html',
})
export class AddmettingPage {
  topic: any;
  desc: any;
  kssj: any;
  userlist = [];
  selectusers;
  mettingid:any;
  filename:any;
  utils = new Utils();
  constructor(public navCtrl: NavController, public navParams: NavParams, public busi: BusiProvider, private native: NativeProvider,
    private fileChooser: FileChooser,private transfer: FileTransfer,private filepath:FilePath) {
    this.kssj = this.utils.getIonCurrentDateTimeString();
    this.getUserList();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AddmettingPage');
  }
  addmetting() {
    if (this.topic == null) {
      this.native.alert("主题不能为空!");
      return;
    }
    if (this.kssj == null) {
      this.native.alert("开始时间不能为空!");
      return;
    }
    let body = {
      topic: this.topic,
      desc: this.desc,
      kssj: this.kssj,
      chr: this.selectusers,
      zlurl:this.filename,
      mettingid:this.mettingid
    }
    this.busi.addMetting(body).subscribe(
      data => {
        this.native.showToast("会议发起成功!");
        this.navCtrl.pop();
      },
      error => {

      }
    );
  }

  getUserList() {
    this.busi.getUserList().subscribe(
      data => {
        this.userlist = data.userlist;
      },
      err => {

      }
    );
  }
  uploadFile() {

    this.fileChooser.open().then(
      data => {
        let options: FileUploadOptions = {
          fileKey: 'file',
          fileName:data.substr(data.lastIndexOf('/')+1),
          headers: {}
        }
        const fileTransfer: FileTransferObject = this.transfer.create();
        this.filepath.resolveNativePath(data).then(
          data=>{
            options.fileName = data.substr(data.lastIndexOf('/')+1);
            this.native.showLoading();
            console.log(options.fileName);
            fileTransfer.upload(data,localStorage.getItem("address")+"metting/uploadZl",options).then(
              data=>{
                this.native.hideLoading();
                let result = JSON.parse(data.response);
                this.filename = result.result.filename;
                this.mettingid = result.result.id;
                this.native.showToast("上传成功!");
              },
              err=>{
                this.native.hideLoading();
              }
            );
          }
        );
      },
      error => {

      }
    );
  }

}
